# -*-  conding: utf-8 -*-
# Author: czh
# Datetime : 2022/5/10 13:36
import time
from pprint import pprint
import pysnowball as ball
import csv
import schedule
from DingDingBot.DDBOT import DingDing

import akshare as ak


def get_symbol():
    df = ak.stock_zh_a_spot()
    df.to_csv('./shishi1.csv')
    symbol_list = []
    """
    获取实时
    0代码,1名称,2最新价,3涨跌额,4涨跌幅,5买入,6卖出,7昨收,8今开,9最高,10最低,11成交量,12成交额
    """
    for idx, data in df.iterrows():
        # print(data)
        symbol = data[0].upper()
        current = data[2]
        open1 = data[8]
        low = data[10]
        high = data[9]
        name = data[1]
        pct_chg = data[4]
        pre_close = data[7]
        """ 计算下影线 / 柱体 大于2.5 踢出ST股票 的股票代码, 涨跌幅小于0的"""
        shiti = abs(open1 - current)
        xiaying = min(open1, current) - low
        shangying = high - max(open1, current)
        chazhi = high - low

        if shiti == 0.0 or xiaying == 0.0 or "ST" in name or current < open1:
            pass
        else:
            jieguo = round(xiaying / shiti, 2)
            jieguo2 = round(shangying / shiti, 2)
            # 振幅
            jieguo3 = round(chazhi / pre_close, 2)
            if jieguo2 < 0.1 and jieguo3 > 0.05 and jieguo > 1:
                # if jieguo > 2 and jieguo3 > 0.06:
                symbol_name = symbol + name
                symbol_list.append(symbol_name)

    return symbol_list


def send_data():
    # current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    # 初始话DingDingBOt webhook是钉钉机器人所必须的
    dd = DingDing(
        webhook='https://oapi.dingtalk.com/robot/send?access_token=b3eb1d427ffc5ba3a08af664866c8bb82cdb846a7783830d8e6c20d5da43c052')

    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
    symbol_list = get_symbol()
    print(symbol_list)
    print("初步筛选数据：", len(symbol_list))
    print(dd.Send_Text_Msg(Content='股票初步筛选数据：{}'.format(symbol_list)))
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))

    new_name_list = []
    for symbol_name in symbol_list:
        sysmbol = symbol_name[:8]
        name = symbol_name[8:]
        # print(symbol_name)
        try:
            res = ball.quotec(sysmbol)
            float_market_capital = float(res["data"][0]["float_market_capital"])
            # 流通值在 150亿以上
            print(name, float_market_capital)
            if float_market_capital > 15000000000:
                new_name_list.append(name)
        except TypeError:
            print("出现异常：", name)

    print(len(new_name_list))
    pprint(new_name_list)
    # 发送文本消息
    print(dd.Send_Text_Msg(Content='股票数据：{}'.format(new_name_list)))
    # 发送链接消息
    try:
        print(dd.Send_Link_Msg(Content=new_name_list[0],
                               Title='股票数据',
                               MsgUrl='http://www.iwencai.com/stockpick/search?tid=stockpick&qs=box_main_ths&w={}'.format(
                                   new_name_list[0]),
                               PicUrl='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14090176146%2F1000.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655428991&t=d4146169032a665cda6e0579d1f2087e'))
    except:
        print("又是啥都没有，我感觉像是有问题一样")
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))


if __name__ == '__main__':
    schedule.every().day.at("14:49").do(send_data)
    while 1:
        schedule.run_pending()
